package packets.client;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.security.PublicKey;

import packets.Packet;

import sun.misc.BASE64Decoder;

/**
 * a data structure that contains 2 {@link PublicKey} public keys: encryption key , and verification key (for digital signature)
 * the client uses this data structure to send his keys to the server when registering
 * meant to be sent in a {@link Packet}
 */
public class PublicKeyPair implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PublicKey _msgKey;
	private PublicKey _sigkey;
	
	/**
	 * Creates an instance of {@link PublicKeyPair}
	 * @param msgKey encryption key
	 * @param sigKey verification key
	 */
	public PublicKeyPair(PublicKey msgKey, PublicKey sigKey) {
		_msgKey = msgKey;
		_sigkey = sigKey;
	}

	/**
	 * 
	 * @return the encryption key stored in this data structure
	 */
	public PublicKey getEncryptionKey() {
		return _msgKey;
	}

	/**
	 * 
	 * @return the verification key stored in this data structure
	 */
	public PublicKey getSigkey() {
		return _sigkey;
	}

	public static PublicKeyPair readServerKeys() throws IOException, ClassNotFoundException{
		String filename = "./serverpublickeys";
		BASE64Decoder d = new BASE64Decoder();
		FileInputStream fis = new FileInputStream(filename);
		byte[] bytes = d.decodeBuffer(fis);
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(bais);
		return (PublicKeyPair)ois.readObject();
	}

}
